package com.yuke.cloud.service.wmc.controller.frontend;

/*
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yuke-mall </br> </p>
 * <p>com.yuke.cloud.service.omc.controller.frontend Description TODO </br> </p>
 * @Author wg
 * @Date   2020/5/2 17:02
 */


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.DateUtil;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.wmc.dto.WmsPurchaseOrderDto;
import com.yuke.cloud.service.wmc.dto.WmsStockInDto;
import com.yuke.cloud.service.wmc.service.WmsPurchaseOrderService;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.yuke.cloud.common.util.excel.ExcelOutUtil.getMergePaperPath;
import static com.yuke.cloud.common.util.excel.ExcelOutUtil.helpdown;
import static com.yuke.cloud.common.util.excel.ZipUtil.fileToZip;

/**
 * 导出报表
 *
 * @return
 */

@RestController
@RequestMapping("/storage")
public class WmsStockInExcelExport extends BaseController {

    @Resource
    private WmsStorageController wmsStorageController;
    @Resource
    private WmsPurchaseOrderService wmsPurchaseOrderService;

    private String fileName="入库信息";



    @RequestMapping("/in/export")
    @ResponseBody
    public void export(
            @ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
            @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
            @ApiParam(name = "warehouseId", value = "仓库id", defaultValue = "0") @RequestParam(required = false) Long warehouseId,
            @ApiParam(name = "specId", value = "产品id", defaultValue = "0") @RequestParam(required = false) Long specId,
            @ApiParam(name = "inType", value = "入库类型", defaultValue = "0") @RequestParam(required = false) Byte inType,
            @ApiParam(name = "beginTime", value = "开始时间", defaultValue = "0") @RequestParam(required = false)  Long beginTime,
            @ApiParam(name = "endTime", value = "结束时间", defaultValue = "0") @RequestParam(required = false)  Long endTime,
            @ApiParam(name = "minPrice", value = "低价", defaultValue = "0") @RequestParam(required = false) Integer minPrice,
            @ApiParam(name = "maxPrice", value = "高价", defaultValue = "0") @RequestParam(required = false) Integer maxPrice,
            @ApiParam(name = "maxPrice", value = "高价", defaultValue = "0") @RequestParam(required = false) Boolean isDelete,
            @ApiParam(name = "purchaseId", value = "单号", defaultValue = "0") @RequestParam(required = false) Long purchaseId
            ,HttpServletRequest request, HttpServletResponse response
    ) throws Exception {

        System.out.println("export开始");

        //excel标题
        String[] title ={ "序号", "所属仓库", "商品名称","商品父类","商品分类", "入库类型", "入库数量","采购价格","入库时间","生产日期",};
        String [] properties = new String[]{"index", "warehouseName", "specName","parentId","categoryName","inType", "inNumber", "supplyPrice","inboundTime","mfgDate"};

        //sheet名
        String sheetName = "出库统计表";


        //不同系统文件夹的生成
        String mergePaperPath = getMergePaperPath(new Date());

        List<File> tempPath = new ArrayList<>();

        boolean flag =true;
        while(flag){
            List<Map> map = new ArrayList<>();
            int index = 1;
            Wrapper<PageInfo<WmsStockInDto>> search = wmsStorageController.search1(page, size, warehouseId, specId, inType,beginTime ,endTime,maxPrice,minPrice,isDelete,purchaseId);

            PageInfo<WmsStockInDto> result = search.getResult();

            List<WmsStockInDto> list = result.getList();

            for (WmsStockInDto wmsStockInDto : list) {
                Map value = JSON.parseObject(JSON.toJSONString(wmsStockInDto), Map.class);
                switch(wmsStockInDto.getInType().toString()){
                    case "0" :value.put("inType","采购入库");
                        break;
                    case "1" :value.put("inType","其他仓库调配入库");
                        break;
                    case "2" :value.put("inType","用户退货入库");
                        break;
                }

                value.put("index",index++);
                if (wmsStockInDto.getInboundTime() != null ){
                    value.put("inboundTime",DateUtil.getStringDate(wmsStockInDto.getInboundTime()));
                }
                if (wmsStockInDto.getMfgDate() != null ){
                    value.put("mfgDate",DateUtil.getStringDate(wmsStockInDto.getMfgDate()));
                }

                map.add(value);
            }

            //创建HSSFWorkbook
            ExcelOutUtil excelExport2 = new ExcelOutUtil();
            excelExport2.setFileName(fileName);
            excelExport2.setData(map);
            excelExport2.setHeardKey(properties);
            excelExport2.setFontSize(14);
            excelExport2.setSheetName(sheetName);
            excelExport2.setTitle(sheetName);
            excelExport2.setHeardList(title);
            String s = excelExport2.exportExport(request, response,page,2,mergePaperPath);

            tempPath.add(new File(s));

            if (list == null || list.size() < 1000){
                flag=false;
                break;
            }

            page++;
        }


        fileToZip(mergePaperPath,mergePaperPath,fileName);
        helpdown(request,response,mergePaperPath+fileName+".zip");

        //删除文件
        for (File temp : tempPath) {
            System.out.println("delete:"+temp);
            temp.delete();
        }

        //删除 压缩包
        new File(mergePaperPath+fileName+".zip").delete();
        new File(mergePaperPath).delete();

    }

    @RequestMapping("/purchase/export")
    @ResponseBody
    public void purchaseExport(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "1") Integer page,
                           @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "1000") Integer size,
                           @ApiParam(name = "wmsPurchaseOrderDto", value = "参数") @RequestBody WmsPurchaseOrderDto wmsPurchaseOrderDto,
                           HttpServletRequest request,HttpServletResponse response) throws IOException {

        //excel标题
        String[] title ={ "序号","进货订单号", "支付类型","订单创建时间","经办人", "应付金额", "实付金额","供应商","支付账号","备注"};

        String [] properties = new String[]{"index", "purchaseId", "payType","createTime","userName","payable", "payment","supplierName","remark"};
        //excel文件名
        String fileName = "订单商品明细" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "订单商品明细";

        //zip文件名
        String zipName = "订单商品明细";

        //zip路径名
        String zipNameString = zipName+".zip";

        //不同系统文件夹的生成
        String mergePaperPath = getMergePaperPath(new Date());

        //存入地址
        List<File> tempPath = new ArrayList<>();

        boolean flag =true;

        while(flag){

            List<Map> map = new ArrayList<>();

            PageHelper.startPage(page, size);

            List<WmsPurchaseOrderDto> list = wmsPurchaseOrderService.selectCondition(wmsPurchaseOrderDto);

            PageInfo<WmsPurchaseOrderDto> pageInfo = new PageInfo<>(list);

            List<WmsPurchaseOrderDto> infoList = pageInfo.getList();

            int index = 1;

            for (WmsPurchaseOrderDto orderGoodsDto : infoList) {

                Map value = JSON.parseObject(JSON.toJSONString(orderGoodsDto), Map.class);

                BigDecimal a = new BigDecimal((float)orderGoodsDto.getPayable()/100);

                Double goodsDouble  = a.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();

                BigDecimal b = new BigDecimal((float)orderGoodsDto.getPayment()/100);

                Double priceDouble  = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();

                switch (orderGoodsDto.getPayType().toString()) {
                    case "0": value.put("payType","未确认");
                        break;
                    case "1": value.put("payType","支付宝");
                        break;
                    case "2": value.put("payType","微信");
                        break;
                    case "3": value.put("payType","银联");
                        break;
                    case "4": value.put("payType","现金");
                        break;

                    default :
                        value.put("payType","未确认");
                }
                value.put("index",index++);

                value.put("payable",goodsDouble);

                value.put("payment",priceDouble);

                value.put("createTime",DateUtil.getStringDate(orderGoodsDto.getCreateTime()));

                map.add(value);
            }


            //创建HSSFWorkbook
            ExcelOutUtil excelExport = new ExcelOutUtil();
            excelExport.setFileName(fileName);
            excelExport.setData(map);
            excelExport.setHeardKey(properties);
            excelExport.setFontSize(14);
            excelExport.setSheetName(sheetName);
            excelExport.setTitle(sheetName);
            excelExport.setHeardList(title);
            String s = excelExport.exportExport(request, response, page, 2, mergePaperPath);

            tempPath.add(new File(s));

            if (list == null || list.size() < size){
                flag=false;
                break;
            }

            page++;
        }


        fileToZip(mergePaperPath,mergePaperPath,zipName);

        helpdown(request,response,mergePaperPath+zipNameString);

        //删除文件
        for (File temp : tempPath) {
            temp.delete();
        }
        //删除 压缩包
        new File(mergePaperPath+zipNameString).delete();
    }




}
